﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace UIT_CV_Management
{
    public class ThongTinPhu
    {
        public String maThongTinPhu;

        public String nguyenQuan;
        public String danToc;
        public String diaChi;
        public String tonGiao;
        public String thanhPhanBanThan;
        public DateTime ngayVaoDang;
        public DateTime ngayVaoDoan;
        public String chucVuCaoNhat;
        public String noiCVCN;
        public DateTime thoiGianCVCN;
        public String tinhTrangSucKhoe;

        //database
        Database myDatabase = Database.shareDatabase;

        /// <summary>
        /// Khời tạo từ những thông tin cơ bản.
        /// </summary>
        /// <param name="_nguyenQuan"></param>
        /// <param name="_danToc"></param>
        /// <param name="_diaChi"></param>
        /// <param name="_tonGiao"></param>
        /// <param name="_thanhPhanBanThan"></param>
        /// <param name="_ngayVaoDang"></param>
        /// <param name="_ngayVaoDoan"></param>
        /// <param name="_chucVuCaoNhat"></param>
        /// <param name="_noiCVCN"></param>
        /// <param name="_thoiGianCVCN"></param>
        /// <param name="_tinhTrangSucKhoe"></param>
        public ThongTinPhu(String _nguyenQuan, String _danToc, String _diaChi, String _tonGiao, String _thanhPhanBanThan, DateTime _ngayVaoDang, DateTime _ngayVaoDoan, String _chucVuCaoNhat, String _noiCVCN, DateTime _thoiGianCVCN, String _tinhTrangSucKhoe)
        {
            maThongTinPhu = Database.getID("ThongTinPhu", "MaThongTinPhu");

            nguyenQuan = _nguyenQuan;
            danToc = _danToc;
            diaChi = _diaChi;
            tonGiao = _tonGiao;
            thanhPhanBanThan = _thanhPhanBanThan;
            ngayVaoDang = _ngayVaoDang;
            ngayVaoDoan = _ngayVaoDoan;
            chucVuCaoNhat = _chucVuCaoNhat;
            noiCVCN = _noiCVCN;
            thoiGianCVCN = _thoiGianCVCN;
            tinhTrangSucKhoe = _tinhTrangSucKhoe;
        }

        /// <summary>
        /// Khởi Tạo đối tượng từ MaThongTinPhu
        /// </summary>
        /// <param name="_maThongTinPhu"></param>
        /// 
        public ThongTinPhu(String _maThongTinPhu)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM ThongTinPhu WHERE MaThongTinPhu = '" + _maThongTinPhu + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;
                DataRow row = table.Rows[0];
                maThongTinPhu = table.Rows[0]["MaThongTinPhu"].ToString();
                nguyenQuan = table.Rows[0]["NguyenQuan"].ToString();
                danToc = table.Rows[0]["DanToc"].ToString();
                diaChi = table.Rows[0]["DiaChi"].ToString();
                tonGiao = table.Rows[0]["TonGiao"].ToString();
                thanhPhanBanThan = table.Rows[0]["ThanhPhanBanThan"].ToString();
                ngayVaoDoan = row.Field<DateTime>("NgayVaoDoan");
                ngayVaoDang = row.Field<DateTime>("NgayVaoDang");
                chucVuCaoNhat = table.Rows[0]["ChucVuCaoNhat"].ToString();
                noiCVCN = table.Rows[0]["NoiCVCN"].ToString();
                thoiGianCVCN = row.Field<DateTime>("ThoiGianCVCN");
                tinhTrangSucKhoe = table.Rows[0]["TinhTrangSucKhoe"].ToString();

            }
        }

        /// <summary>
        /// Lưu toàn bộ thông tin vào Database
        /// </summary>
        public Boolean Save()
        {
            String sqlThongTinPhu = "INSERT INTO ThongTinPhu VALUES('"
                + maThongTinPhu + "',N'"
                + nguyenQuan + "',N'"
                + diaChi + "',N'"
                + danToc + "',N'"
                + tonGiao + "',N'"
                + thanhPhanBanThan + "','"
                + Database.sqlDateTime(ngayVaoDoan) + "','"
                + Database.sqlDateTime(ngayVaoDang) + "',N'"
                + chucVuCaoNhat + "',N'"
                + noiCVCN + "','"
                + Database.sqlDateTime(thoiGianCVCN) + "',N'"
                + tinhTrangSucKhoe + "')";
            QueryResult result = myDatabase.Query(sqlThongTinPhu);

            if (result.ReturnCode == 409)
                return false;
            else
                return true;
                //MessageBox.Show("Lỗi khi lưu dữ liệu Thông Tin Phụ vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("ThongTinPhu", "MaThongTinPhu", maThongTinPhu);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE ThongTinPhu SET "
                 + "NguyenQuan = N'" + nguyenQuan
                 + "', DiaChi = N'" + diaChi
                 + "', DanToc = N'" + danToc
                 + "',TonGiao = N'" + tonGiao
                 + "', ThanhPhanBanThan = N'" + thanhPhanBanThan
                 + "',NgayVaoDoan = '" + Database.sqlDateTime(ngayVaoDoan)
                 + "', NgayVaoDang = '" + Database.sqlDateTime(ngayVaoDang)
                 + "',ChucVuCaoNhat = N'" + chucVuCaoNhat
                 + "',NoiCVCN = N'" + noiCVCN
                 + "',ThoiGianCVCN = '" + Database.sqlDateTime(thoiGianCVCN)
                 + "',TinhTrangSucKhoe = N'" + tinhTrangSucKhoe
                    + "' WHERE MaThongTinPhu = '" + maThongTinPhu + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;
                return true;

            }
            return false;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM ThongTinPhu WHERE MaThongTinPhu = '" + maThongTinPhu + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            return true;
        }
    }
}
